package 
{
	import away3d.materials.ColorMaterial;
	import away3d.materials.PhongBitmapMaterial;
	import away3d.materials.PhongMovieMaterial;
	import away3d.materials.TransformBitmapMaterial;
	import fl.motion.Color;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	
	/**
	 * ...
	 * @author ...
	 */
	public class BarnTexture {
		
		public var name:String;
		public var color:Number;
		public var swatch:Sprite;
		public var trim:ColorMaterial;
		public var roof:TransformBitmapMaterial;
		public var roofBottom:ColorMaterial;
		public var siding:TransformBitmapMaterial;
		
		public function BarnTexture(textureName:String, inColor:Number):void {
			name = textureName;
			var colorTransform:ColorTransform = new ColorTransform();
			colorTransform.color = uint(inColor);
			
			trim = new ColorMaterial(inColor);
			
			var roofTexture:Sprite = new libRoofTexture() as Sprite;
			roofTexture.getChildByName("bgColor").transform.colorTransform = colorTransform;
			var roofBmd:BitmapData = new BitmapData(roofTexture.width, roofTexture.height);
			roofBmd.draw(roofTexture);
			roof = new TransformBitmapMaterial(roofBmd, { repeat:true, precision:2, smooth:true } );
			roof.rotation = 1.570796327;
			roof.scaleX = .5;
			
			var sidingTexture:Sprite = new libSidingTexture() as Sprite;
			sidingTexture.getChildByName("bgColor").transform.colorTransform = colorTransform;
			var bmd:BitmapData = new BitmapData(sidingTexture.width, sidingTexture.height);
			bmd.draw(sidingTexture);
			siding = new TransformBitmapMaterial(bmd, { repeat:true, precision:2, smooth:true } );
			
			roofBottom = createBottomRoofTexture(inColor);
			this.color = inColor;
		}
		
		private function createBottomRoofTexture(color:Number):ColorMaterial {
			var rgbObj:Object = {
				red: ((color & 0xFF0000) >> 16),
				green: ((color & 0x00FF00) >> 8),
				blue: ((color & 0x0000FF))
			};
			
			rgbObj.red = Math.max(rgbObj.red-30, 0);
			rgbObj.green = Math.max(rgbObj.green-30, 0);
			rgbObj.blue = Math.max(rgbObj.blue-30, 0);
			
			var colorValue:Number = rgbObj.red << 16 | rgbObj.green << 8 | rgbObj.blue;
			return new ColorMaterial(colorValue);
		}
		
		public function getSwatch():Sprite {
			var swatch:Sprite = new Sprite();
			swatch.graphics.beginFill(color);
			swatch.graphics.lineStyle(1, 0x000000);
			swatch.graphics.drawRect(0, 0, 20, 15);
			swatch.graphics.endFill();
			return swatch;
		}
		
	}
	
}